package com.tensquare.recruit.controller;

import com.tensquare.recruit.pojo.Recruit;
import com.tensquare.recruit.service.RecruitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:zimmor
 * @Date: 2021/2/28 20:38
 * @Description
 * @version: 1.0.0
 */
@RestController
@RequestMapping("/recruit")
@CrossOrigin
public class RecruitController {
    @Autowired
    private RecruitService recruitService;

    /**
     * 获取最新职位信息
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/search/newlist")
    public Result newList() {
        return new Result(true, StatusCode.OK, "查询成功", recruitService.findByStateNotOrderByCreatetieDesc());
    }

    /**
     * 获取推介列表
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/search/recommend")
    public Result recommend() {
        return new Result(true, StatusCode.OK, "查询成功", recruitService.findByStateOrderByCreatetimeDesc());
    }

    /**
     * 分页查询
     * @param recruit
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/search/{page}/{size}")
    public Result findSearch(@RequestBody Recruit recruit, @PathVariable int page, @PathVariable int size) {
        Page pageData = recruitService.findSearch(recruit, page, size);
        return new Result(true, StatusCode.OK, "分页查询成功", new PageResult<Recruit>(pageData.getTotalElements(), pageData.getContent()));
    }

    /**
     * 条件查询
     * @param recruit
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value="/search")
    public Result findSearch(@RequestBody Recruit recruit) {
        return new Result(true, StatusCode.OK, "条件查询成功", recruitService.findSearch(recruit));
    }

    /**
     * 根据id查询招聘信息
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value="/{id}")
    public Result findById(@PathVariable String id) {
        return new Result(true, StatusCode.OK, "根据id查询成功", recruitService.findById(id));
    }

    /**
     * 修改招聘信息
     * @param id
     * @param recruit
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT, value="/{id}")
    public Result update(@PathVariable String id, @RequestBody Recruit recruit) {
        recruit.setId(id);
        recruitService.update(recruit);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 删除招聘信息
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public Result deleteById(@PathVariable String id) {
        recruitService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 新增招聘信息
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Recruit recruit) {
        recruitService.add(recruit);
        return new Result(true, StatusCode.OK, "新增成功");
    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询所有成功", recruitService.findAll());
    }
}
